﻿Imports System.Windows.Threading
Imports FuncClasses
Imports HtmlAgilityPack
Imports System.Net
Imports System.Collections.ObjectModel

Public Class DHLBusiness
    Implements IPackager

    Private _id As String
    Private _manager As DHLOnlineLogin
    Private _details As New ObservableCollection(Of KeyValuePairs)
    WithEvents uicontrol As New DHLControl

    Public Sub New(loginmanager As IManager, id As String)
        _id = id
        _manager = loginmanager
        AddHandler _manager.GetDetailsCompleted, AddressOf GetDetailsCompleted
        If Not String.IsNullOrEmpty(id) Then
            Try : RefreshState() : Catch ex As Exception : End Try
        End If
    End Sub

    Public ReadOnly Property Details As ObservableCollection(Of FuncClasses.KeyValuePairs) Implements IPackager.Details
        Get
            Return _details
        End Get
    End Property

    Private Sub GetDetailsCompleted(id As String, html As String)
        If id = _id Then
            Try
                'Parse HTML
                Dim doc As New HtmlDocument
                doc.LoadHtml(html)
                Dim link As HtmlNode = doc.DocumentNode.SelectSingleNode("/html/body/div/div/div/div/span[1]/div[1]/table[1]/tbody[1]/tr/td/") ' Tabelle mit allen Infos
                Dim kvp As New KeyValuePairs("", "")

                'Produkt
                Dim item As HtmlNode = link.SelectSingleNode("/table[0]/tbody/tr[0]/td/")
                kvp.Key = item.InnerText.Trim
                'Letzte Meldung
                item = link.SelectSingleNode("/table[0]/tbody/tr[1]/td/")
                kvp.Value = item.InnerText.Trim
                Try
                    'Empfänger
                    item = link.SelectSingleNode("/table[0]/tbody/tr[3]/td/")
                    kvp.Others(1) = item.InnerText.Trim
                    ' Zugestellt an
                    item = link.SelectSingleNode("/table[0]/tbody/tr[4]/td/")
                    kvp.Others(0) = item.InnerText.Trim
                Catch ex As Exception
                    'Not Delivered?
                End Try
                uicontrol.SetLastState(kvp)

                item = link.SelectSingleNode("/table[2]/tbody/")
                _details.Clear()
                For Each child In item.ChildNodes
                    _details.Add(New KeyValuePairs(child.SelectSingleNode("/td[0]/").InnerText.Trim, child.SelectSingleNode("/td[1]/").InnerText.Trim, child.SelectSingleNode("/td[2]/").InnerText.Trim))
                Next
                uicontrol.SetDetails(_details)

                If html.Contains("Icon_05_ZugestelltACTIVE_58x58.gif") Then
                    uicontrol.SetImages(True, True, True, True, True)
                ElseIf html.Contains("Icon_04_ZustellungACTIVE_58x58.gif") Then
                    uicontrol.SetImages(True, True, True, True, False)
                ElseIf html.Contains("Icon_03_PaketzentrumACTIVE_58x58.gif") Then
                    uicontrol.SetImages(True, True, True, False, False)
                ElseIf html.Contains("Icon_02_TransportACTIVE_58x58.gif") Then
                    uicontrol.SetImages(True, True, False, False, False)
                ElseIf Mid(html, html.IndexOf("Icon_01_"), 40).Contains("ACTIVE") Then
                    uicontrol.SetImages(True, False, False, False, False)
                Else
                    uicontrol.SetImages(False, False, False, False, False)
                End If
            Catch : End Try
        End If
    End Sub

    Public Sub GetAllStates() Implements IPackager.GetAllStates
        _manager.GetDetails(_id)
    End Sub

    Public Sub GetLastState() Implements IPackager.GetLastState
        GetAllStates()
    End Sub

    Public ReadOnly Property InfoControl As System.Windows.UIElement Implements IPackager.InfoControl
        Get
            Return uicontrol
        End Get
    End Property

    Public ReadOnly Property LastState As String Implements IPackager.LastState
        Get
            Return _details(_details.Count - 1).Value.ToString
        End Get
    End Property

    Public Property Package_ID As String Implements IPackager.Package_ID
        Get
            Return _id
        End Get
        Set(value As String)
            _id = value
            Try : RefreshState() : Catch ex As Exception : End Try
        End Set
    End Property

    Public ReadOnly Property Packager As Packager Implements IPackager.Packager
        Get
            Return PacketTracking.Packager.DHL_Business
        End Get
    End Property

    Public Sub RefreshState() Implements IPackager.RefreshState
        GetAllStates()
    End Sub
End Class
